// pages/detail/detail.js
let request = require('../../utils/_request');
const app = getApp()
// 全局变量
const {
  baseUrl,
  audioManager //播音组件
} = app.globalData

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: app.globalData.userInfo,
    phoneNum: app.globalData.phoneNum,
    id: 0,
    type: '',
    dealwith: '',
    eventListObj: {
    },
    // 控件
    options: {
      currentClickItem: '',
      recordStatus: [0, 0, 0] //-1暂停,1播放中,0一般
    },
    // 表单数据
    formData: {
      eventName: '',
      eventType: [],
      text: '',
      address: '',
      level: '',
      time: '',
    },
    levelItems: ['一般', '急', '特急'],
    photosList: [],
    videosList: [],
    recordList: [],
    mediaList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type,
      id: options.id,
      dealwith: options.dealwith,
    })
    this.detailInfo(options.id)
    this.initRecord()
    let that = this
    app.watchUserInfo(that.watchBackUserInfo)
    app.watchPhoneNum(that.watchBackPhoneNum)
  },
  watchBackUserInfo(userInfo) {
    console.log('监听到的数据userInfo：' + userInfo)
    let that = this
    app.globalData.userInfo = userInfo
    that.setData({
      userInfo: userInfo
    })
  },
  watchBackPhoneNum(phoneNum) {
    console.log('监听到的数据phoneNum：' + phoneNum)
    let that = this
    app.globalData.phoneNum = phoneNum
    that.setData({
      phoneNum: phoneNum
    })
  },
  // 详情信息
  detailInfo(id) {
    let that = this
    let url = `${baseUrl}/api/eventTodo/detail?todoId=${id}`
    // let url = `${baseUrl}/api/eventTodo/detail?todoId=${id}`

    wx.showLoading({
      title: '加载中...',
    })
    request.$get({
      url: `/api/eventTodo/detail?todoId=${id}`,
      onSuccess: function (res) {
        if (res.data.code === 0) {
          console.log(res.data)
          that.setData({
            eventListObj: res.data.eventTodoVO
          })
          let picUrlList = res.data.eventTodoVO.picUrlList
          let videoUrlList = res.data.eventTodoVO.videoUrlList
          let voiceUrlList = res.data.eventTodoVO.voiceUrlList

          picUrlList = picUrlList ? picUrlList.map(item => {
            return {
              url: baseUrl + '/api/file/downloadFile?pathUrl=' + item,
              isImage: true
            }
          }) : []
          videoUrlList = videoUrlList ? videoUrlList.map(item => {
            return {
              url: baseUrl + '/api/file/downloadFile?pathUrl=' + item,
              isVideo: true
            }
          }) : []
          voiceUrlList = voiceUrlList ? voiceUrlList.map(item => {
            return baseUrl + '/api/file/downloadFile?pathUrl=' + item
          }) : []

          let mediaList = picUrlList.concat(videoUrlList)

          that.setData({
            eventListObj: res.data.eventTodoVO,
            photosList: picUrlList,
            videosList: videoUrlList,
            recordList: voiceUrlList,
            mediaList
          })
        }
        console.log(res)
      },
    })
  },

  // 事件类别
  eventTypeChange(event) {
    const {
      picker,
      value,
      index
    } = event.detail;
    picker.setColumnValues(1, eventTypes[value[0]]);
  },

  // 文本双向绑定
  setFormValue(e) {
    let {
      formData,
      options
    } = this.data
    console.log(e.target)
    console.log(e.detail.value)
    formData[e.target.dataset.name] = e.detail.value
    options.showPopup = false
    this.setData({
      formData,
      options
    })
  },
  // 选择框双向绑定
  bindPickerChange(e) {
    let formData = this.data.formData
    let objectArray = this.data[e.target.dataset.optionsName]
    formData[e.target.dataset.name] = objectArray[e.detail.value].id
    formData[e.target.dataset.name + 'Label'] = objectArray[e.detail.value].name
    this.setData({
      formData
    })
  },


  //  级联选择器列变化
  mulPickerColChange(e) {
    if (e.detail.column === 0) {
      let objectArray2 = this.data.objectArray2
      objectArray2[e.detail.column + 1] = this.data.objectArrayTree[e.detail.value].children
      this.setData({
        objectArray2: objectArray2
      })
    }
  },
  //  级联选择器双向绑定数据
  mulPickerChange(e) {
    let formData = this.data.formData
    let obj = this.data[e.target.dataset.optionsName][e.detail.value.length - 1][e.detail.value[e.detail.value.length - 1]]
    formData[e.target.dataset.name] = obj.id
    formData[e.target.dataset.name + 'Label'] = obj.name
    this.setData({
      formData
    })
    console.log(obj, this.data.formData)
  },
  getMap() {
    let that = this
    wx.chooseLocation({
      success: function (res) {
        let name = res.name
        let address = res.address
        let latitude = res.latitude
        let longitude = res.longitude
        let formData = that.data.formData
        formData.address = res.address
        that.setData({
          formData
        })
        console.log(res)
      }
    })
  },
  /**
   * 初始化语音识别回调
   * 绑定语音播放开始事件
   */
  initRecord: function () {
    let {
      recordList,
      options
    } = this.data
    let that = this
    // 音频播放开始回调
    audioManager.onPlay(() => {
      // wx.showToast({
      //   title: '开始播放',
      //   duration: 500
      // })
    })
    // 音频播放结束回调
    audioManager.onEnded(() => {
      options.recordStatus = [0, 0, 0]
      this.setData({
        options
      })
    })
  },
  //音频播放
  playAudio(e) {
    let options = this.data.options
    let {
      path: src,
      index
    } = e.currentTarget.dataset

    if (audioManager.src === src && options.recordStatus[index] === 1) {
      // 暂停
      audioManager.pause()
      options.recordStatus = [0, 0, 0]
      options.recordStatus[index] = -1
    } else {
      // 播放
      audioManager.src = src
      audioManager.play()
      options.recordStatus = [0, 0, 0]
      options.recordStatus[index] = 1
    }

    this.setData({
      options
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    let type = this.data.type
    let todoId = this.data.id
    let dealwith = this.data.dealwith
    if(res.target.dataset.share ==="dealwithSuccess") {
      return {
        title: '事件处置完成',
        path: `/pages/detailother/detailother?type=${type}&todoId=${todoId}&dealwith=${dealwith}` // 路径，传递参数到指定页面。
       }
    } else {
      return {
        title: '数字人慧勤务平台',
        desc: '数字人慧勤务平台',
        path: `/pages/dealwithDetail/dealwithDetail?type=${type}&id=${todoId}&dealwith=${dealwith}` // 路径，传递参数到指定页面。
       }
    }
  }
})
